package com.yida.gwj.config.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.api.domain.GwjConfBusinessFlag;
import com.yida.gwj.api.domain.GwjConfStep;
import com.yida.gwj.config.mapper.GwjConfBusinessFlagMapper;
import com.yida.gwj.config.service.IGwjConfBusinessFlagService;
import com.yida.gwj.config.service.IGwjConfStepConfigInfoService;
import com.yida.gwj.config.service.IGwjConfStepService;

/**
 * 业务标识Service业务层处理
 *
 * @author lqq
 * @date 2022-12-21
 */
@Service
public class GwjConfBusinessFlagServiceImpl implements IGwjConfBusinessFlagService {
    @Autowired
    private GwjConfBusinessFlagMapper gcBusinessFlagMapper;

    @Autowired
    private IGwjConfStepService iGwjConfStepService;

    @Autowired
    private IGwjConfStepConfigInfoService iGwjConfStepConfigInfoService;

    /**
     * 查询业务标识
     *
     * @param bizFlagId 业务标识主键
     * @return 业务标识
     */
    @Override
    public GwjConfBusinessFlag selectGwjConfBusinessFlagByBizFlagId(Integer bizFlagId) {
        return gcBusinessFlagMapper.selectGwjConfBusinessFlagByBizFlagId(bizFlagId);
    }

    /**
     * 查询业务标识列表
     *
     * @param gcBusinessFlag 业务标识
     * @return 业务标识
     */
    @Override
    public List<GwjConfBusinessFlag> selectGwjConfBusinessFlagList(GwjConfBusinessFlag gcBusinessFlag) {
        return gcBusinessFlagMapper.selectGwjConfBusinessFlagList(gcBusinessFlag);
    }

    /**
     * 新增业务标识
     *
     * @param gcBusinessFlag 业务标识
     * @return 结果
     */
    @Override
    public int insertGwjConfBusinessFlag(GwjConfBusinessFlag gcBusinessFlag) {
        gcBusinessFlag.setCreateTime(DateUtils.getNowDate());
        gcBusinessFlag.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        return gcBusinessFlagMapper.insertGwjConfBusinessFlag(gcBusinessFlag);
    }

    /**
     * 修改业务标识
     *
     * @param gcBusinessFlag 业务标识
     * @return 结果
     */
    @Override
    public int updateGwjConfBusinessFlag(GwjConfBusinessFlag gcBusinessFlag) {
        gcBusinessFlag.setUpdateTime(DateUtils.getNowDate());
        gcBusinessFlag.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        return gcBusinessFlagMapper.updateGwjConfBusinessFlag(gcBusinessFlag);
    }

    /**
     * 批量删除业务标识
     *
     * @param bizFlagIds 需要删除的业务标识主键
     * @return 结果
     */
    @Override
    public int deleteGwjConfBusinessFlagByBizFlagIds(Long[] bizFlagIds) {
        return gcBusinessFlagMapper.deleteGwjConfBusinessFlagByBizFlagIds(bizFlagIds);
    }

    /**
     * 删除业务标识信息
     *
     * @param bizFlagId 业务标识主键
     * @return 结果
     */
    @Override
    public int deleteGwjConfBusinessFlagByBizFlagId(Long bizFlagId) {
        return gcBusinessFlagMapper.deleteGwjConfBusinessFlagByBizFlagId(bizFlagId);
    }

    @Override
    public List<GwjConfBusinessFlag> selectGwjConfBusinessFlag(GwjConfBusinessFlag gcBusinessFlag) {
        return gcBusinessFlagMapper.selectGwjConfBusinessFlagList(new GwjConfBusinessFlag());
    }

    /* 
     * 根据bizFlagId获取详细配置信息 20230829
     */
    @Override
    public JSONObject selectGwjConfBusinessFlagInfoByBizFlagId(Integer bizFlagId) {
        JSONObject json = new JSONObject();
        // 1获取flag
        GwjConfBusinessFlag gcbf = gcBusinessFlagMapper.selectGwjConfBusinessFlagByBizFlagId(bizFlagId);
        json.put("flagInfo", gcbf);
        // 获取配置流程及步骤
        List<GwjConfStep> stepList = iGwjConfStepService.selectGwjConfStepListByFlagId(bizFlagId);
        // 获取步骤配置信息
        JSONArray stepArray = new JSONArray();
        // stepList.forEach(gcf -> gcf.setConfigs());
        for (GwjConfStep gcs : stepList) {
            JSONObject stepJson = new JSONObject();
            Long stepId = gcs.getStepId();
            String stepName = gcs.getName();
            int sort = gcs.getSort();
            // JSONArray configList = new JSONArray();
            List<String> configCodeList = iGwjConfStepConfigInfoService.selectGwjConfStepConfigInfoCodeListByStepId(stepId);
            configCodeList = configCodeList.stream().filter(s -> !s.startsWith("auto")).collect(Collectors.toList());
            stepJson.put("stepId", stepId);
            stepJson.put("stepName", stepName);
            stepJson.put("sort", sort);
            stepJson.put("configList", configCodeList);
            stepArray.add(stepJson);
        }
        json.put("confStepList", stepArray);
        return json;
    }
}
